package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/lab/constant"
	"github.com/lab-online/internal/lab/entity"
	"github.com/lab-online/pkg/auth"
	"github.com/lab-online/pkg/logger"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
)

type getUploadURLReqParams struct {
	ClassID      uint `form:"classID" binding:"required,min=1" example:"1"`         // 班级ID
	ExperimentID uint `form:"experimentID" binding:"required,min=1" example:"1"` // 实验ID
}

var GetUploadURLValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Query: &getUploadURLReqParams{},
	}
})

type getUploadURLRespData struct {
	URL string `json:"url" binding:"required"`
}

type GetReportRespBody struct {
	resp.BaseRespBody
	Data *getUploadURLRespData `json:"data" binding:"required"`
}

// GetUploadURL 获取 oss 上传 URL
//
//	@Summary				获取 oss 上传 URL
//	@Description.markdown	GetUploadURL
//	@Tags					labs
//	@Tags					mimo
//	@Accept					json
//	@Produce				json
//	@Param					Authorization	header		string					true	"Bearer Token"
//	@Param					report			query		getUploadURLReqParams	true	"报告信息"
//	@Failure				400				{object}	resp.BaseRespBody
//	@Failure				401				{object}	resp.BaseRespBody
//	@Success				200				{object}	GetReportRespBody
//	@Security				bearer
//	@Router					/v1/labs/upload [get]
func (h *Handler) GetUploadURL(c *gin.Context) {
	authEntity := c.MustGet(auth.KeyAuth).(auth.AuthEntity)
	params := c.MustGet(mw.KeyQuery).(*getUploadURLReqParams)

	lab := entity.New(
		entity.WithStudent(authEntity.GetUsername()),
		entity.WithClassID(params.ClassID),
		entity.WithExperimentID(params.ExperimentID),
	)

	if url, err := h.domain.CreateUploadURL(lab); err != nil {
		logger.Error("create upload url", err)
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeUploadURLGenSuccess, &getUploadURLRespData{
			URL: url,
		})
	}
}
